package com.pengxh.app.realityshow.netty;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.pengxh.app.multilib.base.BaseNormalActivity;
import com.pengxh.app.multilib.widget.EasyToast;
import com.pengxh.app.realityshow.R;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author Pengxh
 * @description: 测试长连接
 * @date :2019/11/15 17:17
 */
public class NettyActivity extends BaseNormalActivity implements View.OnClickListener {

    private static final String TAG = "NettyActivity";

    @BindView(R.id.inputIp)
    EditText inputIp;
    @BindView(R.id.inputPort)
    EditText inputPort;
    @BindView(R.id.startConnect)
    Button startConnect;
    @BindView(R.id.mServerMsg)
    TextView mServerMsg;

    private UpdateViewBroadcastReceiver updateBroadcast = null;

    @Override
    public void initView() {
        setContentView(R.layout.activity_netty);
    }

    @Override
    public void initData() {
        if (updateBroadcast == null) {
            updateBroadcast = new UpdateViewBroadcastReceiver();
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("action.updateView");
            registerReceiver(updateBroadcast, intentFilter);
        }
        //启动长连接服务
        startService(new Intent(this, LongConnectService.class));
    }

    @Override
    public void initEvent() {

    }

    @OnClick(R.id.startConnect)
    @Override
    public void onClick(View v) {
        String tag = startConnect.getText().toString().trim();
        if (tag.equals("启动长连接")) {
            String ip = inputIp.getText().toString();
            String port = inputPort.getText().toString();

            if (ip.equals("") || port.equals("")) {
                EasyToast.showToast("啥都还没输入呢！", EasyToast.ERROR);
            } else {
                //发送配置信息广播
                Intent intent = new Intent();
                intent.setAction("action.getConfig");
                intent.putExtra("ip", ip);
                intent.putExtra("port", port);
                sendBroadcast(intent);
            }
        } else if (tag.equals("清理数据")) {
            mServerMsg.setText("");
        } else {
            Log.e(TAG, "onClick: ", new Throwable());
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (updateBroadcast != null) {
            unregisterReceiver(updateBroadcast);
        }
    }

    class UpdateViewBroadcastReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Log.d(TAG, "onReceive: " + action);
            if (action != null && action.equals("action.updateView")) {
                String serverMsg = intent.getStringExtra("serverMsg");
                Log.d(TAG, "接收到服务器消息: " + serverMsg);

                mServerMsg.append(serverMsg + "\r\n");
                if (!mServerMsg.getText().toString().equals("")) {
                    startConnect.setText("清理数据");
                }
            } else {
                Log.e(TAG, "onReceive: ", new Throwable());
            }
        }
    }
}